<?php

/**
 * 发现管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class Find extends Common
{
    public function initialize()
    {
        parent::initialize();
    }


    // 帖子
    public function lists(){
        if(Request::isAjax()){
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $where = ['d.del'=>0,'d.is_report'=>0];
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('find_dynamic')->alias('d')
                ->field('d.*,u.nickname,c.title category_name')
                ->join('users u','u.id = d.uid')
                ->join('goods_category c','c.id = d.category')
                ->where($where)->where('d.content', 'like', "%" . $key . "%")
                ->paginate($paginate)->toArray();
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    public function putaway(){
        $id = input('id');
        $put = input('putaway');
        $res = db('find_dynamic')->where('id',$id)->update(['putaway'=>$put]);
        if($res){
            return ['code'=>1,'msg'=>'操作成功！'];
        }else{
            return ['code'=>0,'msg'=>'操作失败！'];
        }
    }

    // 添加帖子
    public function addLists(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            db('find_dynamic')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['url'] = url('notice');
            return $result;
        }else{
            $this->assign('title','添加帖子');
            $this->assign('info','null');
            return $this->fetch();
        }
    }

    // 查看帖子
    public function lookLists(){
        $id = input('id');
        $info = db('find_dynamic')->alias('d')->json(['pics'])
            ->field('d.*,u.nickname,c.title category_name')
            ->join('users u','u.id = d.uid')
            ->join('goods_category c','c.id = d.category')
            ->where('d.id',$id)
            ->find();
        $goods_name = db('goods_info')->where('id',$info['goods_id'])->value('name');
        $info['goods_name'] = $goods_name;
        $this->assign('info',$info);
        $this->assign('title','查看帖子');
        return $this->fetch('look');
    }

    // 修改帖子
    public function editLists(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            db('find_dynamic')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('notice');
            return $result;
        }else{
            $id = input('id');
            $info = db('find_dynamic')->where('id',$id)->find();
            $this->assign('info',json_encode($info,true));
            $this->assign('title','修改帖子');
            return $this->fetch('add_lists');
        }
    }
    // 置顶帖子
    public function topLists(){
        $id = input('id');
        db('find_dynamic')->where('id',$id)->update(['top'=>1]);
        $result['code'] = 1;
        $result['msg'] = '置顶成功!';
        return $result;
    }
    // 删除帖子
    public function delLists(){
        $id = input('id');
        db('find_dynamic')->where('id',$id)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    // 批量删除帖子
    public function delAllLists(){
        $map[] =array('id','in',input('param.ids/a'));
        db('find_dynamic')->where($map)->update(['del'=>1]);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        return $result;
    }


    // 被举报的帖子
    public function report(){
        if(Request::isAjax()){
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $where = ['del'=>0,'is_report'=>1];
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('find_dynamic')->where($where)->where('content', 'like', "%" . $key . "%")->paginate($paginate)->toArray();
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
    // 删除帖子
    public function delReport(){
        $id = input('id');
        db('find_dynamic_comment')->where('id',$id)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功！'];
    }


    // 评论
    public function comment(){
        if(Request::isAjax()){
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $where = ['d.del'=>0];
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('find_dynamic_comment')->alias('d')
                ->field('d.*,u.nickname')
                ->join('users u','u.id = d.uid')
                ->where($where)->where('d.content', 'like', "%" . $key . "%")
                ->paginate($paginate)->toArray();
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    // 删除帖子
    public function delComment(){
        $id = input('id');
        db('find_dynamic_comment')->where('id',$id)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    // 批量删除帖子
    public function delAllComment(){
        $map[] =array('id','in',input('param.ids/a'));
        db('find_dynamic_comment')->where($map)->update(['del'=>1]);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        return $result;
    }

}